package api;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.cfg.WxCfg;
import wxPaySdk.WXPay;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * 标注微信回调加锁    签名验证
 */
@Service
public class WxPayApi {

    @Autowired
    WxCfg wxCfg;

    @Autowired
    WXPay wxPay;

    private static final Logger LOG = LoggerFactory.getLogger(WxPayApi.class);

    /**
     * 微信支付统一下单
     *
     * @param
     * @param amount
     * @return <xml>
     * <return_code><![CDATA[SUCCESS]]></return_code>
     * <return_msg><![CDATA[OK]]></return_msg>
     * <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
     * <mch_id><![CDATA[10000100]]></mch_id>
     * <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str>
     * <openid><![CDATA[oUpF8uMuAJO_M2pxb1Q9zNjWeS6o]]></openid>
     * <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign>
     * <result_code><![CDATA[SUCCESS]]></result_code>
     * <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id>
     * <trade_type><![CDATA[JSAPI]]></trade_type>
     * </xml>
     * @throws Exception
     */
    public Map<String, String> unifiedOrder(String amount, String openId, String orderCode) throws Exception {
        Map map = new HashMap();
        map.put("attach", "微客脉");
        map.put("body", "pay");
        map.put("out_trade_no", orderCode);
        map.put("total_fee", amount);
        map.put("spbill_create_ip", getIp());
        map.put("notify_url", wxCfg.getNotify_url());
        map.put("trade_type", "JSAPI");
        map.put("openid", openId);
        return wxPay.unifiedOrder(map);
    }

    /**
     * 作用：查询订单<br>
     * 场景：刷卡支付、公共号支付、扫码支付、APP支付
     *
     * @param orderCode
     * @return API返回数据
     * <xml>
     * <return_code><![CDATA[SUCCESS]]></return_code>
     * <return_msg><![CDATA[OK]]></return_msg>
     * <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
     * <mch_id><![CDATA[10000100]]></mch_id>
     * <device_info><![CDATA[1000]]></device_info>
     * <nonce_str><![CDATA[TN55wO9Pba5yENl8]]></nonce_str>
     * <sign><![CDATA[BDF0099C15FF7BC6B1585FBB110AB635]]></sign>
     * <result_code><![CDATA[SUCCESS]]></result_code>
     * <openid><![CDATA[oUpF8uN95-Ptaags6E_roPHg7AG0]]></openid>
     * <is_subscribe><![CDATA[Y]]></is_subscribe>
     * <trade_type><![CDATA[MICROPAY]]></trade_type>
     * <bank_type><![CDATA[CCB_DEBIT]]></bank_type>
     * <total_fee>1</total_fee>
     * <fee_type><![CDATA[CNY]]></fee_type>
     * <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>
     * <out_trade_no><![CDATA[1415757673]]></out_trade_no>
     * <attach><![CDATA[订单额外描述]]></attach>
     * <time_end><![CDATA[20141111170043]]></time_end>
     * <trade_state><![CDATA[SUCCESS]]></trade_state>
     * </xml>
     * @throws Exception
     */
    public Map<String, String> orderQuery(String orderCode) throws Exception {
        Map map = new HashMap();
        map.put("out_trade_no",orderCode);
        return wxPay.orderQuery(map);
    }

    /**
     * 作用：关闭订单<br>
     * 场景：公共号支付、扫码支付、APP支付
     * 以下情况需要调用关单接口：商户订单支付失败需要生成新单号重新发起支付，要对原订单号调用关单，避免重复支付；系统下单后，用户支付超时，系统退出不再受理，避免用户继续，请调用关单接口。
     * 注意：订单生成后不能马上调用关单接口，最短调用时间间隔为5分钟。
     *
     * @param
     * @return API返回数据
     * <xml>
     * <return_code><![CDATA[SUCCESS]]></return_code>
     * <return_msg><![CDATA[OK]]></return_msg>
     * <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
     * <mch_id><![CDATA[10000100]]></mch_id>
     * <nonce_str><![CDATA[BFK89FC6rxKCOjLX]]></nonce_str>
     * <sign><![CDATA[72B321D92A7BFA0B2509F3D13C7B1631]]></sign>
     * <result_code><![CDATA[SUCCESS]]></result_code>
     * <result_msg><![CDATA[OK]]></result_msg>
     * </xml>
     * @throws Exception
     */
    public Map<String, String> closeOrder(String orderCode) throws Exception {
        return wxPay.closeOrder(ImmutableMap.of("out_trade_no", orderCode));
    }

    /**
     * 作用：申请退款<br>
     * 场景：刷卡支付、公共号支付、扫码支付、APP支付
     *
     * @param orderCode 原订单号
     * @param newCode   新订单号
     * @param fee       总额
     * @param refundFee 退款金额
     * @return API返回数据
     * <xml>
     * <return_code><![CDATA[SUCCESS]]></return_code>
     * <return_msg><![CDATA[OK]]></return_msg>
     * <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
     * <mch_id><![CDATA[10000100]]></mch_id>
     * <nonce_str><![CDATA[NfsMFbUFpdbEhPXP]]></nonce_str>
     * <sign><![CDATA[B7274EB9F8925EB93100DD2085FA56C0]]></sign>
     * <result_code><![CDATA[SUCCESS]]></result_code>
     * <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>
     * <out_trade_no><![CDATA[1415757673]]></out_trade_no>
     * <out_refund_no><![CDATA[1415701182]]></out_refund_no>
     * <refund_id><![CDATA[2008450740201411110000174436]]></refund_id>
     * <refund_channel><![CDATA[]]></refund_channel>
     * <refund_fee>1</refund_fee>
     * </xml>
     * @throws Exception
     */
    public Map<String, String> refund(String orderCode, String newCode, String fee, String refundFee,String des) throws Exception {
        Map map = new HashMap();
        map.put("out_trade_no", orderCode);
        map.put("out_refund_no", newCode);
        map.put("total_fee", fee);
        map.put("refund_fee",refundFee);
        map.put("refund_desc",des);
       /* map.put("notify_url","");*/
        return wxPay.refund(map);
    }

    /**
     * 作用：退款查询<br>
     * 场景：刷卡支付、公共号支付、扫码支付、APP支付
     *
     * @param orderCode 订单号
     * @return API返回数据
     * <xml>
     * <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
     * <mch_id><![CDATA[10000100]]></mch_id>
     * <nonce_str><![CDATA[TeqClE3i0mvn3DrK]]></nonce_str>
     * <out_refund_no_0><![CDATA[1415701182]]></out_refund_no_0>
     * <out_trade_no><![CDATA[1415757673]]></out_trade_no>
     * <refund_count>1</refund_count>
     * <refund_fee_0>1</refund_fee_0>
     * <refund_id_0><![CDATA[2008450740201411110000174436]]></refund_id_0>
     * <refund_status_0><![CDATA[PROCESSING]]></refund_status_0>
     * <result_code><![CDATA[SUCCESS]]></result_code>
     * <return_code><![CDATA[SUCCESS]]></return_code>
     * <return_msg><![CDATA[OK]]></return_msg>
     * <sign><![CDATA[1F2841558E233C33ABA71A961D27561C]]></sign>
     * <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id>
     * </xml>
     * @throws Exception
     */
    public Map<String, String> refundQuery(String orderCode) throws Exception {
        return wxPay.refund(ImmutableMap.of("out_trade_no", orderCode));
    }



    public double totalMoney(Double num) {
        return num * 100;
    }

    public String getIp() {
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            return "127.0.0.1";
        }
        String ip = addr.getHostAddress().toString();
        return ip;
    }
} // end class
